﻿using Cesave.Infrastructure.CrossCutting.Network;
using System.Net.Sockets;
using Cesave.Infrastructure.CrossCutting.NetFramework.Properties;

namespace Cesave.Infrastructure.CrossCutting.NetFramework.Network
{
    public class NetworkHelper
        : INetworkHelper
    {
        #region INetworkHelper Members

        public bool IsConnectedToInternet
        {
            get
            {
                return ConnectedToInternet();
            }
        }

        public bool IsConnectedToSynchronizationServer
        {
            get
            {
                return ConnectedToSynchronizationServer();
            }
        }

        #endregion

        #region Private properties

        private bool ConnectedToSynchronizationServer()
        {
            try
            {
                TcpClient client = new TcpClient(Settings.Default.SynchronizationHost, Settings.Default.SynchronizationPort);
                return client.Connected;
            }
            catch (SocketException)
            {
                return false;
            }
        }

        private bool ConnectedToInternet()
        {
            try
            {
                TcpClient client = new TcpClient(Settings.Default.InternetHost, Settings.Default.InternetPort);
                return client.Connected;
            }
            catch (SocketException)
            {
                return false;
            }
        }

        #endregion

    }
}
